package com.youlu.campus.admin.course.service;

import com.youlu.campus.admin.course.dto.*;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.course.ActivityPopup;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author : [dengbo]
 * @className : ActivityPopupAdminService
 * @description : [弹窗类]
 * @createTime : [2022/11/30 11:15]
 */
@Service
@Slf4j
public class ActivityPopupAdminService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 弹窗保存
     *
     * @param saveDTO
     * @return
     */
    public boolean save(ActivityPopupSaveDTO saveDTO) {
        ActivityPopup popup = null;
        if (StringUtils.isNotBlank(saveDTO.getId())) {
            //修改
            popup = mongoTemplate.findOne(Query.query(Criteria.where("id").is(saveDTO.getId())), ActivityPopup.class);
            if (popup == null) {
                throw new BusinessException("弹窗信息不存在");
            }
        }
        if (popup == null) {
            popup = new ActivityPopup();
            popup.setCreatedTime(new Date());
            popup.setDeleted(false);
        }
        popup.setName(saveDTO.getName());
        popup.setRedirectUrl(saveDTO.getRedirectUrl());
        popup.setImageUrl(saveDTO.getImageUrl());
        popup.setPopupType(saveDTO.getPopupType());
        popup.setStartTime(saveDTO.getStartTime());
        popup.setEndTime(saveDTO.getEndTime());
        mongoTemplate.save(popup);
        return true;
    }

    /**
     * 弹窗查询
     *
     * @param dto
     * @return
     */
    public Page<ActivityPopup> list(ActivityPopupSearchDTO dto) {
        Query query = new Query();
        if (StringUtils.isNotBlank(dto.getName())) {
            query.addCriteria(Criteria.where("name").is(dto.getName()));
        }
        Date date = new Date();
        if (dto.getValid() == 1) {
            query.addCriteria(Criteria.where("startTime").lte(date));
            query.addCriteria(Criteria.where("endTime").gte(date));
        } else if (dto.getValid() == 2) {
            Criteria criteria = new Criteria();
            criteria.orOperator(Criteria.where("startTime").gt(date),Criteria.where("endTime").lt(date));
            query.addCriteria(criteria);
        }
        query.addCriteria(Criteria.where("deleted").is(false));
        long count = mongoTemplate.count(query, ActivityPopup.class);
        query.with(Sort.by(Sort.Order.desc("createdTime")));
        PageRequest pageRequest = PageRequest.of(dto.getPage() - 1, dto.getPageSize());
        List<ActivityPopup> datas = mongoTemplate.find(query.with(pageRequest), ActivityPopup.class);
        return PageableExecutionUtils.getPage(datas, pageRequest, () -> count);
    }

    /**
     *  删除记录
     *
     * @param saveDTO
     * @return
     */
    public boolean delete(ActivityPopupDeleteDTO saveDTO) {
        Query query = Query.query(Criteria.where("id").is(saveDTO.getId()));
        Update update = new Update();
        update.set("deleted", true);
        mongoTemplate.updateFirst(query, update, ActivityPopup.class);
        return true;
    }
}
